﻿using System.Collections.Generic;
using System.Windows.Controls;
using PSE.Deployment.SetupBase.Configuration;

namespace PSE.Deployment.ConfigurationManager.Controls
{
	/// <summary>
	/// UserControl que instancia um botao de Adição de tag
	/// </summary>
	public class AddTagButton : Button
	{
		SimpleGroupBoxes _parentGrid;
		XmlTag _tag = null;
		List<XmlTag> _allTags = null;

		public AddTagButton(List<XmlTag> allTags, XmlTag tag, SimpleGroupBoxes parentGrid)
		{
			_parentGrid = parentGrid;
			_tag = this.GenerateNewTag(tag);
			_allTags = allTags;

			this.Content = "Incluir (+)";
			this.SetResourceReference(AddTagButton.StyleProperty, "ButtonCommon");
			this.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
			this.VerticalAlignment = System.Windows.VerticalAlignment.Top;

			this.Click += new System.Windows.RoutedEventHandler(AddTagButton_Click);
		}

		/// <summary>
		/// Tratamento do evento click do controle
		/// </summary>
		void AddTagButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			XmlTag copiedtag = this.CopyEmptyTag(_tag);

			SimpleGroupBox sgb = new SimpleGroupBox(_allTags, copiedtag, _parentGrid);
			sgb.SetValue(Grid.RowProperty, _parentGrid.GRID_GROUP_BOXES_ROW_COUNTER);

			_parentGrid.RowDefinitions.Add(new RowDefinition());
			_parentGrid.Children.Add(sgb);

			_allTags.Add(copiedtag);

			_parentGrid.GRID_GROUP_BOXES_ROW_COUNTER++;

			_parentGrid.ForbidAllowLastItemRemoval();
		}

		/// <summary>
		/// Metodo que gera uma tag com valores vazios
		/// </summary>
		/// <param name="tag"></param>
		/// <returns></returns>
		XmlTag GenerateNewTag(XmlTag tag)
		{
			XmlTag newTag = new XmlTag();
			newTag.Attributes = new List<XmlAttribute>();

			foreach (XmlAttribute at in tag.Attributes)
			{
				XmlAttribute newAttribute = new XmlAttribute();
				newAttribute.Name = at.Name;
				newAttribute.Description = at.Description;
				newAttribute.Type = at.Type;
				newTag.Attributes.Add(newAttribute);
			}

			newTag.Description = tag.Description;
			newTag.IsList = tag.IsList;
			newTag.Name = tag.Name;
			newTag.OperationType = tag.OperationType;
			newTag.SQLCommand = tag.SQLCommand;
			newTag.Type = tag.Type;
			newTag.Value = tag.Value;
			newTag.XPathList = tag.XPathList;

			return newTag;
		}

		XmlTag CopyEmptyTag(XmlTag tag)
		{
			XmlTag copiedTag = new XmlTag();
			copiedTag.Attributes = new List<XmlAttribute>();

			foreach (XmlAttribute at in tag.Attributes)
			{
				XmlAttribute newAttribute = new XmlAttribute();
				newAttribute.Name = at.Name;
				newAttribute.Description = at.Description;
				newAttribute.Type = at.Type;
				copiedTag.Attributes.Add(newAttribute);
			}

			copiedTag.Description = tag.Description;
			copiedTag.IsList = tag.IsList;
			copiedTag.Name = tag.Name;
			copiedTag.OperationType = tag.OperationType;
			copiedTag.SQLCommand = tag.SQLCommand;
			copiedTag.Type = tag.Type;
			copiedTag.Value = "";
			copiedTag.XPathList = tag.XPathList;

			return copiedTag;
		}
	}
}